#!/bin/bash
jarfile="target/blog-1.3.0-GA.jar"
app_args="--spring.profiles.active=product --server.port=9090"
# jvm选项参数
jvm_options="-Xmx2g -Xms128m -Xss512k -XX:+UseG1GC -XX:MaxGCPauseMillis=10 -XX:+HeapDumpOnOutOfMemoryError -XX:+HeapDumpBeforeFullGC -XX:HeapDumpPath=jvm/heap.dump -Xlog:gc*=debug,gc+task*=info:file=jvm/gc%t.log:utctime,level,tags:filecount=50,filesize=100M"
# jvm日志输出目录
jvmLogDir="jvm"
if [ ! -e ${jvmLogDir} ]
then
  mkdir -m 664 ${jvmLogDir}
fi

pid=`ps -ef | grep "${jarfile}" | grep -v grep| awk '{print $2}'` # 现存服务的pid
function start_func(){
	echo "即将启动服务: ${jarfile}"
	if [ -z "${pid}" ] # 检查字符串是否为空
	then
    echo "nohup java ${jvm_options} -jar ${jarfile} ${app_args} &"
    nohup java ${jvm_options} -jar ${jarfile} ${app_args} &
    echo "服务启动成功: ${jarfile}"
  else
    echo "服务早已启动: pid: ${pid}"
  fi
  ps -ef | grep ${jarfile} | grep -v "grep"
}
function stop_func(){
	echo "即将关闭服务: ${jarfile}"
  if [ ! -z "${pid}" ] # 检查字符串是否为空
  then
    echo "关闭服务: ${jarfile}, pid: ${pid}"
    kill ${pid}
    echo "成功关闭, pid: ${pid}"
    pid=""
  else
    echo "服务不存在: ${jarfile}"
  fi
}

if [ $1 = "start" ]
then
  start_func
elif [ $1 = "stop" ]
then
	stop_func
elif [ $1 = "restart" ]
then
  echo "即将重启服务: ${jarfile}"
	stop_func
  sleep 5s
  start_func
else
	echo "非法参数"
	exit 1
fi      # 这个fi必须得有
